package com.cmwa.solution.utils.cached;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.cmwa.solution.sys.organization.entity.DeptmentRelationshipVo;
import com.cmwa.solution.sys.organization.service.DeptmentRelationshipService;
import com.cmwa.solution.sys.userInfo.service.UserInfoService;
import com.cmwa.solution.utils.cached.service.ShareCachedService;

@Component
@SuppressWarnings({"rawtypes"})
public class DepartmentCached implements ShareCachedService, InitializingBean {
	@Resource
	private UserInfoService userInfoService;
	
	@Resource
	private DeptmentRelationshipService deptmentRelationshipService;
	
	/**
	 * 所有部门id和部门name
	 */
	
	private static Map<String, String> depIdAndNameCached = new HashMap<String, String>(1024);
	
	/**
	 * 部门信息缓存集合
	 */
	private static List<DeptmentRelationshipVo> departmentList = new Vector<DeptmentRelationshipVo>();

	@Override
	public void afterPropertiesSet() throws Exception {
		loadMappingCached();
	}

	@Override
	public void loadMappingCached() {
		depIdAndNameCached.clear();
		departmentList.clear();
		/**
		 * 部门信息缓存集合
		 */
		departmentList = deptmentRelationshipService.queryAllDepartmentList();
		for (DeptmentRelationshipVo deptmentRelationshipVo : departmentList) {
			depIdAndNameCached.put(deptmentRelationshipVo.getOrgid(), deptmentRelationshipVo.getOrgname());
		}
	}

	/**
	 * {@link} IBF_GET_DEPTNAME_BY_ID()
	 * 根据部门或组织的ID取得部门名称或组织名称
	 * @param depId
	 * @return
	 */
	public static String getName(String depId) {
		return depIdAndNameCached.get(depId);
	}
	/**
	 * 根据多个部门id取得部门姓名，多个DPEPT Id用,隔开
	 * @param  deptIds
	 * @return
	 */
	public static String getAllName(String deptIds) {
		if (StringUtils.isEmpty(deptIds)) {
			return "";
		}
		String[] deptIdList=deptIds.split(",");
		StringBuffer allNames=new StringBuffer();
		for (int i = 0; i < deptIdList.length; i++) {
			String prefix=",";
			if (i==0) 
				prefix="";
			
			String name=depIdAndNameCached.get(deptIdList[i]);
			allNames.append(prefix+(name==null? "":name));
		}
		return allNames.toString();
	}
	
	/**
	 * 获取所有的部门信息
	 * 
	 * @return List
	 */
	public List getdepartments() {
		return departmentList;
	}

	/**
	 * 根据部门名称获取部门信息
	 * 
	 * @param orgName
	 * @return
	 */
	public static List<DeptmentRelationshipVo> getDepartmentsByName(String orgName,boolean isvalid,boolean leavedept) {
 		List<DeptmentRelationshipVo> vList = new Vector<DeptmentRelationshipVo>();
		if ((departmentList == null || departmentList.size() == 0)) {
			return vList;
		} else {
			for (int i = 0; i < departmentList.size(); i++) {
				DeptmentRelationshipVo vo = (DeptmentRelationshipVo) departmentList.get(i);
				if(StringUtils.isNotEmpty(vo.getOrgnamePath())){
					String orgnamepath = vo.getOrgnamePath();
				    if (orgnamepath.indexOf(orgName) > -1) {
					    vList.add(vo);
				    }
				}
		    }
			//选择有效的部门
		    if(isvalid){
				List<DeptmentRelationshipVo> resList = validdept(vList);
				vList.clear();
				vList = resList;
			}
			
			//选择叶子部门
			if(leavedept){
				List<DeptmentRelationshipVo> resList = leavedept(vList);
				vList.clear();
				vList = resList;
			}	
			return vList;
		}
	}
	
	/**
	 * 根据部门名称获取部门信息(只获取一级部门,过滤掉二级部门)
	 */
	public static List<DeptmentRelationshipVo> getFirstDeptsByName(String orgName,boolean isvalid,boolean leavedept) {
		List<DeptmentRelationshipVo> vList = new Vector<DeptmentRelationshipVo>();
		if ((departmentList == null || departmentList.size() == 0)) {
			return vList;
		} else {
			for (int i = 0; i < departmentList.size(); i++) {
				DeptmentRelationshipVo vo = (DeptmentRelationshipVo) departmentList.get(i);
				if (vo == null) {
					continue;
				}
				
				if(StringUtils.isNotEmpty(vo.getDeptlevel())){
					if(Integer.parseInt(vo.getDeptlevel()) < 2){
						if(StringUtils.isNotEmpty(vo.getOrgnamePath())){
							String orgnamepath = vo.getOrgnamePath();
						    if (orgnamepath.indexOf(orgName) > -1) {
							    vList.add(vo);
						    }
						}
					}
				}
			}
			
			//选择有效的部门
			if(isvalid){
					List<DeptmentRelationshipVo> resList = validdept(vList);
					vList.clear();
					vList = resList;
			}	
			//选择叶子部门
			if(leavedept){
					List<DeptmentRelationshipVo> resList = leavedept(vList);
					vList.clear();
					vList = resList;
			}
			return vList;
		}
	}
	
	//过滤只选取叶子部门
	private static List<DeptmentRelationshipVo> leavedept(List<DeptmentRelationshipVo> vList){
		List<DeptmentRelationshipVo> leaveList = new Vector<DeptmentRelationshipVo>();
        for(DeptmentRelationshipVo vo :  vList){
        	if(StringUtils.isNotEmpty(vo.getConnectByIsleaf()) && "1".equals(vo.getConnectByIsleaf())){
        		leaveList.add(vo);
        	}
        }
	    return leaveList;
	}
	
	//过滤只选取有效的部门
	private static List<DeptmentRelationshipVo> validdept(List<DeptmentRelationshipVo> vList){
		List<DeptmentRelationshipVo> validList = new Vector<DeptmentRelationshipVo>();
		for(DeptmentRelationshipVo vo : vList){
				if(StringUtils.isNotEmpty(vo.getStat()) && "STAR".equals(vo.getStat())){
					validList.add(vo);
				}
		}
		return validList;
	}
}
